package per.ethan.javase.thread;

import java.util.Random;

/**
 * @author Ethan
 * @date 2020/04/27 10:44
 */
public class Demo4 implements Runnable {

    private int ticketNum = 1000;

    public static void main(String[] args) {
        Demo4 t1 = new Demo4();

        new Thread(t1, "张三").start();
        new Thread(t1, "李四").start();
        new Thread(t1, "王五").start();
        new Thread(t1, "赵六").start();
    }

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (ticketNum <= 0) {
                    break;
                }

                try {
                    Thread.sleep(new Random().nextInt(100));

                    System.out.println(Thread.currentThread().getName() + "-> 余票" + --ticketNum);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
